package pl.edu.romanow.util

import net.liftweb.http.SHtml
import net.liftweb.http.js.JsCmds.Function
import net.liftweb.http.js.JE.{JsRaw, Str}
import scala._
import net.liftweb.http.js.{JsExp, JsCmds, JsCmd}

/**
 * Created by IntelliJ IDEA.
 * User: jar
 * Date: 25.03.12
 * Time: 14:27
 * To change this template use File | Settings | File Templates.
 */

object HTMLUtil {

  def refreshParent: JsCmd = JsCmds.Run("if(window.opener){window.opener.location.reload();}")

  def refresh: JsCmd = JsCmds.Run("window.location.reload();")

  def ajaxCall(cmd: => JsCmd): JsCmd = SHtml.ajaxCall(Str("ignore"), {
    ignore: String => cmd
  })._2.cmd

  def ajaxJsFunction(name: String,
                     params: List[String],
                     body: List[String] => JsCmd,
                     formatter: List[String] => JsExp = list => JsRaw(list.mkString("+':'+"))): JsCmd =
    Function(name, params, SHtml.ajaxCall(formatter(params), st => {
      body(st.split(":").toList)
    })._2.cmd)

}
